﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Gacn.ControleAlunos.Library.DataAccess
{
    public static class Util
    {
        /// <summary>
        /// Retorna a conexão que foi definida pela connection string do Web.Config
        /// </summary>
        /// <returns></returns>
        public static SqlConnection GetConnection()
        {
            return new SqlConnection(ConfigurationManager.ConnectionStrings["Desenvolvimento"].ConnectionString);
        }

        public static string DataTableToCSV(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();


            for (int i = 0; i < dt.Columns.Count; i++)
            {
                sb.Append(dt.Columns[i]);
                if (i != dt.Columns.Count)
                    sb.Append(",");
            }

            sb.AppendLine();

            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    sb.Append(dr[i]);
                    if (i != dr.Table.Columns.Count)
                        sb.Append(",");
                }
                sb.AppendLine();
            }
            
            return sb.ToString();
        }
    }
}
